<?php

namespace App\Http\Requests\Admin\Customer\Product;

use App\Http\Requests\ApiRequest;
use App\Http\Requests\Concerns\ProductRule;
use App\Models\Panx\Category;
use App\Models\Panx\Product;
use App\Models\Panx\ProductUrlType;
use App\Services\Utils\Guard;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;

/**
 * @property UploadedFile thumb
 * @property UploadedFile|null color_thumb
 * @property array product_tag_ids
 * @property array spec_values
 * @property int product_url_type_id
 * @property string product - last url path segment product/{product} as product_id
 * @property array body_position_ids
 * @property string category_id
 */
class ProductUpdateRequest extends ApiRequest
{
    use ProductRule;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $customer_id = Guard::customerAdmin()->customer_id;
        $prod_id = $this->product;

        $rule = [
            'name'                => 'required|string|max:'.self::MAX_STRING,
            'title'               => 'required|string|max:'.self::MAX_STRING,
            'description'         => 'nullable|string|max:'.self::MAX_STRING,
            'category_id'         => 'required|int|min:1',
            'brand_id'            => 'required|int|min:0',
            'sell_price'          => 'required|numeric|max:1000000.00',
            'market_price'        => 'nullable|numeric|max:1000000.00',
            // 编码不用限制只能写英文数字， 模型组需要写中文
            'spu_code'     => ['required', 'string', 'max:'.self::MAX_STRING,
                $this->ruleCustomerUnique($customer_id)->ignore($prod_id)
            ],
            'stock'               => 'sometimes|required|int|min:0|max:'.self::MAX_INT,
            'content'             => 'nullable|string',
            'priority'            => 'nullable|int|between:0,10000',

            'is_deleted'          => 'nullable|int|in:0,1',
            'is_sale'             => 'nullable|int|in:0,1',
        ];

        $thumb_rule = $this->thumbUpdateRules();
        $url_rule = $this->normalizeYouZanProductUrlUpdateRule($this->urlRules());

        $category_rule = $this->categoryRelateRules($this->category_id);

        return $thumb_rule + $url_rule + $category_rule + $rule;
    }

}
